<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8" style="margin-bottom: 30px">
      <el-col :span="1.5">
        <span style="font-size: 30px; font-weight: bold">打印开关 </span>
        <el-switch
          v-model="info.value"
          active-color="#13ce66"
          inactive-color="#ff4949"
          @change="updateOff(info)"
        >
        </el-switch>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getOrderList"
      ></right-toolbar>
    </el-row>

    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="商品名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递单号" prop="waybillNo">
        <el-input
          v-model="queryParams.waybillNo"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人姓名" prop="consigneeName">
        <el-input
          v-model="queryParams.consigneeName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人电话" prop="consigneePhone">
        <el-input
          v-model="queryParams.consigneePhone"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人地址" prop="consigneeAddress">
        <el-input
          v-model="queryParams.consigneeAddress"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递状态" prop="printStatus">
        <el-select v-model="queryParams.printStatus" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" style="font-size: 28px; font-weight: 500">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
        >批量打印</el-button>
      </el-col>
  
    </el-row>

    <el-table
      border
      v-loading="loading"
      :data="goodsList"
      @selection-change="handleSelectionChange"
    >
    <el-table-column type="selection" width="55" align="center" />
      <!-- userName   createTime-->
      <el-table-column label="序号" type="index" width="55" align="center">
      </el-table-column>

      <el-table-column label="下单人" align="center" prop="userName" />
      <el-table-column label="下单时间" align="center" prop="createTime" />
      <!--  -->
      <el-table-column label="商品名称" align="center" prop="title">
        <template slot-scope="scope">
          <!-- {{ scope.row.orderDetails[0] || scope.row.orderDetails[0].title }} -->
          {{ scope.row.orderDetails.map((item) => item.title).join(",") }}
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="url">
        <template slot-scope="scope">
          <!-- <image-preview :src="scope.row.url" :width="50" :height="50" />
            -->
          {{ scope.row.orderCode }}
        </template>
      </el-table-column>
      <el-table-column label="快递单号" align="center" prop="pid">
        <template slot-scope="scope">
          {{ scope.row.waybillNo }}
        </template>
      </el-table-column>

      <el-table-column label="收货人姓名" align="center" prop="consigneeName">
        <template slot-scope="scope">
          {{ scope.row.orderConsignee.consigneeName }}
        </template>
      </el-table-column>
      <el-table-column label="收货人电话" align="center" prop="consigneePhone">
        <template slot-scope="scope">
          {{ scope.row.orderConsignee.consigneePhone }}
        </template>
      </el-table-column>

      <el-table-column label="收货人地址" align="center" prop="createTime">
        <template slot-scope="scope">
          {{ scope.row.orderConsignee.consigneeAddress
          }}{{ scope.row.orderConsignee.addressDetailed }}
        </template>
      </el-table-column>

      <el-table-column label="快递单状态" align="center" prop="createTime">
        <template slot-scope="scope">
          {{ scope.row.printStatus == 0 ? "未打印" : "已打印" }}
        </template>
      </el-table-column>

      <el-table-column label="物流状态" align="center" prop="createTime">
        <template slot-scope="scope">
          <el-button type="primary" @click="lookWu(scope.row)">查看</el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            slot="reference"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="print(scope.row)"
          >
            重新打印</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="物流状态"
      :visible.sync="openWl"
      width="40%"
      append-to-body
    >
      <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in activities"
          :key="index"
          color="green"
          :timestamp="activity.processTime"
        >
          {{ activity.processInfo }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getOrderList"
    />
  </div>
</template>

<script>
import { route } from "../../../utils/socketRoute";
import {
  listConfig,
  getConfig,
  delConfig,
  addConfig,
  updateConfig,
} from "@/api/api/config";

import {
  dictList,
  getOrderList,
  printAgain,
  cancel,
  saleCount,
  getProcess,
} from "@/api/api/cate";

export default {
  name: "Config",
  data() {
    return {
      openWl: false,
      activities: [],
      info: {},
      printArr:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 配制表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      options: [
        {
          value: "0",
          label: "未打印",
        },
        {
          value: "1",
          label: "已打印",
        },
      ],
      // 查询参数
      queryParams: {
        title:'',
        id: 2,
        pageNum: 1,
        pageSize: 10,
        label: null,
        key: "print_off",
        userName: "",
        createTime: "",
        orderCode: "",
        waybillNo: "",
        consigneeName: "",
        consigneePhone: "",
        consigneeAddress: "",
        printStatus: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      goodsList: [],
    };
  },
  created() {
    this.getList();
    this.getOrderList();
  },
  methods: {
    getOrderList() {
      this.loading = true;
      this.queryParams.status = "";
      getOrderList(this.queryParams).then((response) => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询配制列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then((response) => {
        this.configList = response.rows;
        this.configList.forEach((obj) => {
          obj.value = obj.value == "1" ? true : false;
        });
        this.info = this.configList[0];
        this.total = response.total;
        this.loading = false;
      });
    },
    print(val) {

      let params = {
        type: "1",
        data: {
          code: val.waybillNo,
          url: `/profile/upload/pdf/${val.waybillNo.substring(0, 2).toLowerCase()}/${val.waybillNo}.pdf`,
        },
      };
      route(params);
      setTimeout(()=>{
        this.getOrderList()
      },1000)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    lookWu(val) {
      this.activities = []
      getProcess(val.id).then((res) => {
        this.activities = res.data.data;
        this.openWl = true;
      });
   
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        label: null,
        key: null,
        value: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getOrderList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.printArr = selection
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (this.printArr.length == 0) {
        this.$modal.msgError("请选择打印的订单");
        return;
      }else{
        this.printArr.forEach(i=>{
          this.print(i)
        })
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getConfig(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改配制";
      });
    },
    updateOff(row) {
      row.value = row.value ? 1 : 0;
      updateConfig(row).then((response) => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateConfig(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除配制编号为"' + ids + '"的数据项？')
        .then(function () {
          return delConfig(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "api/config/export",
        {
          ...this.queryParams,
        },
        `config_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
